#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "util.h"
#include<QMessageBox>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(400,500);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_bcls_clicked()
{
    ui->lineEdit->clear();
    ui->lcdNumber->display(0);
}

void MainWindow::on_backspace_clicked()
{
    auto t = ui->lineEdit->text();
    ui->lineEdit->setText(t.left(t.length()-1));
}

void MainWindow::appendLineEditText(QString str){
    QString s = ui->lineEdit->text();
    ui->lineEdit->setText(s+str);
}



void MainWindow::on_bleftquto_clicked()
{
    appendLineEditText("(");
}

void MainWindow::on_brightquto_clicked()
{
    appendLineEditText(")");
}

void MainWindow::on_b3_clicked()
{
    appendLineEditText("3");
}

void MainWindow::on_b1_clicked()
{
    appendLineEditText("1");
}

void MainWindow::on_b2_clicked()
{
    appendLineEditText("2");
}

void MainWindow::on_badd_clicked()
{
    appendLineEditText("+");
}

void MainWindow::on_b4_clicked()
{
    appendLineEditText("4");
}

void MainWindow::on_b5_clicked()
{
    appendLineEditText("5");
}

void MainWindow::on_b6_clicked()
{
    appendLineEditText("6");
}

void MainWindow::on_bsub_clicked()
{
    appendLineEditText("-");
}

void MainWindow::on_b7_clicked()
{
    appendLineEditText("7");
}

void MainWindow::on_b8_clicked()
{
    appendLineEditText("8");
}

void MainWindow::on_b9_clicked()
{
    appendLineEditText("9");
}

void MainWindow::on_bmul_clicked()
{
    appendLineEditText("*");
}

void MainWindow::on_bdot_clicked()
{
    appendLineEditText(".");
}

void MainWindow::on_b0_clicked()
{
    appendLineEditText("0");
}


QString clearSpace(QString expr){
//    QMessageBox::information(nullptr,"expression",expr);
    QString ret;
    for(int i=0;i<expr.length();i++){
        if(!QChar::isSpace(expr[i].toLatin1())){
            ret.append(expr[i]);
        }
    }

    return ret;
}

void MainWindow::on_beq_clicked()
{
//    QString s("2+3+(4+5*6)*7+100-10/2-2");
    ui->lcdNumber->display(evalExpresion(clearSpace(ui->lineEdit->text())));

}

void MainWindow::on_bdiv_clicked()
{
    appendLineEditText("/");
}
